//
//  DialogChooseView.m
//  CarDeal_IOS
//
//  Created by 孙可 on 2018/9/4.
//  Copyright © 2018年 sunke. All rights reserved.
//

#import "DialogChooseView.h"
#import "DialogChooseCell.h"

@implementation DialogChooseView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = translucentBg;
        _uiView = [[UIView alloc] init];
        _uiView.layer.cornerRadius = 10;
        _uiView.layer.masksToBounds = YES;
        _uiView.backgroundColor = [UIColor whiteColor];
        
        _tvTitle = [[UILabel alloc] init];
        _tvTitle.textColor = title_color;
        _tvTitle.font = textFont;
        _tvTitle.textAlignment = NSTextAlignmentCenter;
        
        _tableChooseView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
        [_tableChooseView registerClass:[DialogChooseCell class] forCellReuseIdentifier:@"cellID"];
        _tableChooseView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableChooseView.bounces = NO;
        _tableChooseView.dataSource = self;
        _tableChooseView.delegate = self;
        
        [self addSubview:_uiView];
        [_uiView addSubview:_tvTitle];
        [_uiView addSubview:_tableChooseView];
    }
    return self;
}

-(instancetype)initWith:(CGSize)frame title :(NSString *)tittle withArray : (NSArray *) array andType : (int) type{
    self = [super init];
    _tvTitle.text = tittle;
    _dataArray = array;
    _type = type;
    [_uiView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.size.mas_equalTo(frame);
    }];
    [_tvTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_uiView);
        make.left.right.equalTo(_uiView);
        make.height.mas_equalTo(@50);
    }];
    [_tableChooseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(_uiView);
        make.top.equalTo(_tvTitle.mas_bottom);;
    }];
    [_tableChooseView reloadData];
    return self;
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self removeFromSuperview];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DialogChooseCell * cell = [_tableChooseView dequeueReusableCellWithIdentifier:@"cellID"];
    if(_type == 1){
        NSObject * orgEntity = [_dataArray objectAtIndex:indexPath.row];
        cell.tvName.text = [orgEntity valueForKey:_propertyName];
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 49;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self removeFromSuperview];
    if(_didSelect){
       _didSelect([_dataArray objectAtIndex:indexPath.row]);
    }
}

@end
